package com.ybt.router.tcp.receiver;

import org.apache.log4j.Logger;

import com.ybt.communicate.core.exception.ProtocolException;
import com.ybt.communicate.tcp.dispacher.ITCPServerLongDispatcher;
import com.ybt.router.core.message.IMSDMessage;
import com.ybt.router.core.receiver.IReceiver;
import com.ybt.router.core.receiver.dispatch.IMessageDispatch;
import com.ybt.router.core.receiver.dispatch.impl.ByteMessageDispatch;
import com.ybt.router.core.receiver.exception.MessageReceiverException;
import com.ybt.router.tcp.message.util.TCPMessageUtil;

public class TCPMessageReceiver implements IReceiver,ITCPServerLongDispatcher {
    private static Logger logger = Logger.getLogger(TCPMessageReceiver.class);
    private IMessageDispatch dispatch;
    
    public TCPMessageReceiver() {
        super();
        dispatch  = new ByteMessageDispatch();
    }

    public void start() throws MessageReceiverException {
        // TODO Auto-generated method stub

    }

    public void stop() throws MessageReceiverException {
        // TODO Auto-generated method stub

    }

    public void dispatch(Object data) throws ProtocolException {
        // TODO Auto-generated method stub
        if(logger.isDebugEnabled()){
            logger.debug("receive a message, dispatch it...");
        }
        try {
            IMSDMessage<byte[]> msg = TCPMessageUtil.byteArray2message((byte[])data);
            dispatch.onMessage(msg);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new ProtocolException("dispatch the message error",e);
        }
        
    }
    

}
